#include "Common.h"

#include <stdarg.h>
#include <stddef.h>

#include <unistd.h>
#include <time.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "Sock/AppLibSock_Unix.hpp"

/**********************************************************
 * Function Name:        AppLibSockUnix_SrvCreate
 * Description:          创建unix socket服务器
 * Parameters[in]:       IN APP_LIB_SOCKET_TYPE_e enType
 *                       IN CHAR *pcFileName
 *                       INOUT APP_LIB_SOCK_s *pstSrv
 * Parameters[out]:      INOUT APP_LIB_SOCK_s *pstSrv
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.16
***********************************************************/
ULONG AppLibSockUnix_SrvCreate(IN APP_LIB_SOCKET_TYPE_e enType, IN CHAR *pcFileName, INOUT APP_LIB_SOCK_s *pstSrv)
{
    ULONG uRet = STAT_SUCCESS;

    if (pstSrv == NULL)
    {
        return STAT_ERR_INVAILD_PARAM;
    }

    uRet = AppLibSock_InitUnixAddr(pcFileName, &pstSrv->stAddr);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    pstSrv->enType = enType;
    uRet = AppLibSock_SrvCreate(pstSrv, APP_LIB_SOCK_LISTEN_MAX);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    return STAT_SUCCESS;
}

/**********************************************************
 * Function Name:        AppLibSockUnix_CliCreate
 * Description:          创建unix socket客户端
 * Parameters[in]:       IN APP_LIB_SOCKET_TYPE_e enType
 *                       IN CHAR * pcFileName
 *                       INOUT APP_LIB_SOCK_s *pstCli
 * Parameters[out]:      INOUT APP_LIB_SOCK_s *pstCli
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.16
***********************************************************/
ULONG AppLibSockUnix_CliCreate(IN APP_LIB_SOCKET_TYPE_e enType, IN CHAR * pcFileName, INOUT APP_LIB_SOCK_s *pstCli)
{
    ULONG  uRet = STAT_SUCCESS;

    if (pstCli == NULL)
    {
        return STAT_ERR_INVAILD_PARAM;
    }

    uRet = AppLibSock_InitUnixAddr(pcFileName, &pstCli->stAddr);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    pstCli->enType = enType;
    uRet = AppLibSock_Socket(pstCli);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    return STAT_SUCCESS;
}
